package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.AddressFeign;
import com.qf.pojo.User;
import com.qf.pojo.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * fallbackMethod 降级方法，降级指的是我们在遇到问题的时候提供的备选方案
 * @author wbzs,
 * @date 2022/10/19,
 * @time 23:00,
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private AddressFeign addressFeign;
    @RequestMapping("info/{id}")
    public User findByid( @PathVariable Integer id){
        System.out.println(Thread.currentThread().getName());

        User user = new User();
        user.setId(id);
        user.setName("张飞");
        //需要地址信息，地址需要找地址的程序获取
        /**
         * 参数一：我们的请求地址
         * 参数二：我们期望网络请求返回的数据解析为什么类型
         * 参数三：有没有key=value格式的参数
         *
         */
        UserAddress addressByid = addressFeign.findAddressByid(id);
        user.setUserAddress(addressByid);
        return user;

    }
    @RequestMapping("/infos/{id}")
    public User get(@PathVariable Integer id){
        System.out.println(Thread.currentThread().getName());
        User user= new User();
        user.setId(id);
        user.setName("上海");
        UserAddress userAddress = addressFeign.findByid(id, "山东");
        user.setUserAddress(userAddress);
        return user;

    }


    @RequestMapping("/test1")
    @HystrixCommand(fallbackMethod = "test1jianyi")
    public String test1(){

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test1"+System.currentTimeMillis();
    }

    @RequestMapping("/test2")
    public String test2(){



        return "test222222"+System.currentTimeMillis();
    }

    @RequestMapping("/test3")
    //使用的是信号量隔离
    @HystrixCommand(fallbackMethod = "test3fallback",commandProperties = {@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")})
    public String test3(){
        System.out.println(Thread.currentThread().getName());
//
//        if (new Random().nextBoolean()){
//            int i=1/0;
//        }
        System.err.println("test3执行了");
        int i=1/0;
        return "test3"+System.currentTimeMillis();
    }

    public String test1jianyi(){

        return "test1dejianyi";
    }

    public String test3fallback(){

        return "test3dejianyi";
    }


}
